package com.wpre.lotterysystem.controller;

import com.wpre.lotterysystem.common.errcode.ControllerErrorCodeConstants;
import com.wpre.lotterysystem.common.exception.ControllerException;
import com.wpre.lotterysystem.common.pojo.CommonResult;
import com.wpre.lotterysystem.common.utils.JacksonUtil;
import com.wpre.lotterysystem.controller.param.CreatePrizeParam;
import com.wpre.lotterysystem.controller.param.PageParam;
import com.wpre.lotterysystem.controller.result.FindPrizeListResult;
import com.wpre.lotterysystem.service.PictureService;
import com.wpre.lotterysystem.service.PrizeService;
import com.wpre.lotterysystem.service.dto.PageListDTO;
import com.wpre.lotterysystem.service.dto.PrizeDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-20
 * Time: 18:50
 */
@Slf4j
@RestController
public class PrizeController {
    @Autowired
    private PictureService pictureService;

    @Autowired
    private PrizeService prizeService;


    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile file) {
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     * RequestPart,用于接收表单数据 multipart/form-data格式的,RequestBody是接收json字符串格式的
     *
     * @param param
     * @param picFile
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@RequestPart("param") @Validated CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile picFile) {
        log.info("createPrize-CreatePrizeParam", JacksonUtil.writeValueAsString(param));

        return CommonResult.success(prizeService.createPrize(param, picFile));

    }

    //获取奖品列表
    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param) {
        log.info("findPrizeList PageParam param {}", JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> prizeList = prizeService.findPrizeList(param);

        return CommonResult.success(convertToFindPrizeListResult(prizeList));
    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> prizeList) {
        if (prizeList == null) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(prizeList.getTotal());

        result.setRecords(prizeList.getRecords().stream().map(prizeDTO -> {
                    FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                    prizeInfo.setPrizeName(prizeDTO.getName());
                    prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                    prizeInfo.setDescription(prizeDTO.getDescription());
                    prizeInfo.setPrice(prizeDTO.getPrice());
                    prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                    return prizeInfo;
                }).collect(Collectors.toList())
        );
        return result;
    }


}
